
   // 自调用函数-----游戏
   (function () {
    var that = null;
    function Game(map) {
        this.food = new Food();
        this.snake = new Snake();
        this.map = map;
        that = this;
    }
    Game.prototype.init = function () {
        this.food.init(this.map);
        this.snake.init(this.map);
        this.runSnake(this.food, this.map);
        this.bindKey()
    };
    Game.prototype.runSnake = function (food, map) {
        var timeId = setInterval(function () {
            this.snake.move(food, map);
            this.snake.init(map);
            var maxX = map.offsetWidth / this.snake.width;
            var maxY = map.offsetHeight / this.snake.height;
            var headX = this.snake.body[0].x;
            var headY = this.snake.body[0].y;
            if (headX < 0 || headX > maxX) {
                clearInterval(timeId);
                alert("游戏结束");
            }
            if (headY < 0 || headY > maxY) {
                clearInterval(timeId);
                alert("游戏结束");
            }
        }.bind(that), 150)
    }
    Game.prototype.bindKey=function(){
        document.addEventListener("keydown",function(e){
            switch(e.keyCode){
                case 37:that.snake.drection="left";break;
                case 38:that.snake.drection="top";break;
                case 39:that.snake.drection="right";break;
                case 40:that.snake.drection="bottom";break;
            }
        }.bind(that),false)
    }
    window.Game = Game;
}());